import Vue from 'vue';
import VueRouter from 'vue-router';
import Cookies from 'js-cookie';
import 'normalize.css/normalize.css'; // a modern alternative to CSS resets
import Element from 'element-ui';
import './styles/element-variables.scss';
import '@/styles/index.scss'; // global css
import '@geoscene/resources-table/lib/index/index.css';
import { parseTime, resetForm, addDateRange } from '@/utils/ruoyi';
import { getDicts } from '@/api/system/dict/data';
import ResourceTable from '@geoscene/resources-table/lib/index';
import Pagination from '@/components/Pagination/index.vue';
import RightToolbar from '@/components/RightToolbar/index.vue';
import store from './store';
import { constantRoutes } from './router';
import './icons'; // icon
import setRouterGuard from './permission';
import * as filters from './filters';
import plugins from './plugins'; // plugins

import App from './App.vue';
// 字典数据组件
// import DictData from '@/components/DictData/index';

// DictData.install();

Vue.prototype.addDateRange = addDateRange;
Vue.prototype.parseTime = parseTime;
Vue.prototype.resetForm = resetForm;
Vue.prototype.getDicts = getDicts;

if (process.env.NODE_ENV === 'production') {
  const { mockXHR } = require('../mock');
  mockXHR();
}
Vue.use(plugins);

Vue.use(ResourceTable);
Vue.component('Pagination', Pagination);
Vue.component('RightToolbar', RightToolbar);
Vue.use(Element, {
  size: Cookies.get('size') || 'small',
});

Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key]);
});

Vue.config.productionTip = false;

// 微前端--qiankun
Vue.use(VueRouter);
let instance = null;
let router = null;
function render(props) {
  const { container, routeBase } = props || {};
  router = new VueRouter({
    base: window.__POWERED_BY_QIANKUN__ ? routeBase : process.env.VUE_APP_PUBLIC_PATH,
    mode: 'history',
    routes: constantRoutes,
  });
  // console.log('home router', router);
  // 添加路由守卫
  setRouterGuard(router);
  instance = new Vue({
    router,
    store,
    render: (h) => h(App),
  }).$mount(container ? container.querySelector('#child-app') : '#child-app');
}

if (!window.__POWERED_BY_QIANKUN__) {
  render();
}

/**
 * 作为微应用启动时的报错信息，独立运行项目时不算报错
 * @param {*} message
 */
function MicroAppBootstrapErrorMessage(message) {
  if (window.__POWERED_BY_QIANKUN__) {
    throw new Error(message);
  } else {
    console.warn(`当前为独立应用启动状态，请忽略报错信息`, message);
  }
}

export async function bootstrap(props) {
  console.log('[bootstrap]', props);
}

export async function mount(props) {
  console.log('[Vue] props from main framework', props);
  // 主应用传递过来的全局路由
  const { globalRouter, exposeRouter } = props;
  if (globalRouter) {
    Vue.prototype.$globalRouter = globalRouter;
  }
  // 绑定允许跳转的路由表到全局
  if (exposeRouter) {
    Vue.prototype.$exposeRoutes = exposeRouter;
  }
  // 注册微应用和独立运行项目时是否作为报错
  Vue.prototype.$MicroAppBootstrapErrorMessage = MicroAppBootstrapErrorMessage;
  render(props);
}
export async function unmount(props) {
  console.log('[unmount]', props);
  instance.$destroy();
  instance.$el.innerHTML = '';
  instance = null;
  router = null;
  Vue.prototype.$globalRouter = null;
  Vue.prototype.$exposeRoutes = null;
  Vue.prototype.$MicroAppBootstrapErrorMessage = null;
}
